package com.atguigu.eduorder.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-01-08
 */
@RestController
@RequestMapping("/eduorder/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    //根据课程id和用户id创建订单，返回订单id
    @PostMapping("createOrder/{courseId}")
    public R save(@PathVariable String courseId, HttpServletRequest request) {
        String orderId = orderService.saveOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderId", orderId);
    }
    //根据订单id查询订单信息
    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("item",order);

    }

//    //根据课程id和用户id查询订单表中订单状态
//    @GetMapping("isBuyCourse/{courseId}/{memberId}")
//    public  boolean isBuyCourse(@PathVariable String courseId,@PathVariable String memberId){
//        QueryWrapper<Order> wrapper = new QueryWrapper<>();
//        wrapper.eq("course_id",courseId);
//        wrapper.eq("member_id",memberId);
//        wrapper.eq("status",1);
//        int count = orderService.count(wrapper);
//        return count>0;
//    }
    //查询订单表中订单的状态 判断课程是否购买
    @ApiOperation( "判断课程是否购买")
    @GetMapping("isBought/{courseId}")
    public R isBuyByCourseId(@PathVariable String courseId, HttpServletRequest request) {
        String jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        Boolean isBought = orderService.isBoughtByCourseId(courseId, jwtInfo);
        return R.ok().data("isBought", isBought);
    }

    //获取当前用户订单列表
    @ApiOperation(value = "获取当前用户订单列表")
    @GetMapping("orderList")
    public R list(HttpServletRequest request) {
        String jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        List<Order> list = orderService.selectByMemberId(jwtInfo);
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "删除订单")
    @DeleteMapping("removeOrder/{orderId}")
    public R remove(@PathVariable String orderId, HttpServletRequest request) {
        String jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        boolean result = orderService.removeById(orderId, jwtInfo);
        if(result){
            return R.ok().message("删除订单成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

}

